package com.beiben.experiment.equipment.controller;

import java.util.List;

import com.beiben.experiment.equipment.domain.StateVo;
import com.beiben.experiment.repair.service.IRepairService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.beiben.experiment.equipment.domain.Equipment;
import com.beiben.experiment.equipment.service.IEquipmentService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * 试验设备\计量设备信息Controller
 * 
 * @author 熊国强
 * @date 2020-11-11
 */
@RestController
@RequestMapping("/experiment/equipment")
public class EquipmentController extends BaseController
{
    @Autowired
    private IEquipmentService equipmentService;


    /**
     * 查询试验设备\计量设备信息列表
     */
    @PreAuthorize("@ss.hasPermi('experiment:equipment:list')")
    @GetMapping("/list")
    public TableDataInfo list(Equipment equipment)
    {
        startPage();
        List<Equipment> list = equipmentService.selectEquipmentList(equipment);
        return getDataTable(list);
    }

    /**
     * 导出试验设备\计量设备信息列表
     */
    @PreAuthorize("@ss.hasPermi('experiment:equipment:export')")
    @Log(title = "试验、计量设备信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Equipment equipment)
    {
        List<Equipment> list = equipmentService.selectEquipmentList(equipment);
        ExcelUtil<Equipment> util = new ExcelUtil<Equipment>(Equipment.class);
        return util.exportExcel(list, "equipment");
    }

    /**
     * 获取试验设备\计量设备信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('experiment:equipment:query')")
    @GetMapping(value = "/{equipmentId}")
    public AjaxResult getInfo(@PathVariable("equipmentId") Long equipmentId)
    {
        return AjaxResult.success(equipmentService.selectEquipmentById(equipmentId));
    }

    /**
     * 新增试验设备\计量设备信息
     */
    @PreAuthorize("@ss.hasPermi('experiment:equipment:add')")
    @Log(title = "试验、计量设备信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Equipment equipment)
    {
        return toAjax(equipmentService.insertEquipment(equipment));
    }

    /**
     * 修改试验设备\计量设备信息
     */
    @PreAuthorize("@ss.hasPermi('experiment:equipment:edit')")
    @Log(title = "试验、计量设备信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Equipment equipment)
    {
        return toAjax(equipmentService.updateEquipment(equipment));
    }

    /**
     * 删除试验设备\计量设备信息
     */
    @PreAuthorize("@ss.hasPermi('experiment:equipment:remove')")
    @Log(title = "试验、计量设备信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{equipmentIds}")
    public AjaxResult remove(@PathVariable Long[] equipmentIds)
    {
        return toAjax(equipmentService.deleteEquipmentByIds(equipmentIds));
    }

    /**
     * 导入试验设备\计量设备信息
     */
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        ExcelUtil<Equipment> util = new ExcelUtil<Equipment>(Equipment.class);
        return util.importTemplateExcel("试验计量数据");
    }

    @Log(title = "试验、计量设备导入", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('experiment:equipment:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<Equipment> util = new ExcelUtil<Equipment>(Equipment.class);
        List<Equipment> equipmentList = util.importExcel(file.getInputStream());
        String message = equipmentService.importEquipment(equipmentList, updateSupport);
        return AjaxResult.success(message);
    }

    /**
     * 修改试验设备\计量设备信息 使用状态
     */
//    @PreAuthorize("@ss.hasPermi('experiment:equipment:state')")
    @PostMapping("/changeState")
    public AjaxResult changeState(@RequestBody StateVo stateVo)
    {
        return toAjax(equipmentService.updateEquipmentState(stateVo.getIds(),stateVo.getState()));
    }
}
